﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tracker.Services;
using Tracker.Data.DataTransfer;
using Tracker.Views;
using Tracker.Helper;
using System.ComponentModel;

namespace Tracker.Presenters
{
    public class OptionPresenter
    {
        #region private fields

        private ITaskData _td;
        private IOptionView _ov;
        
        #endregion

        public OptionPresenter(ITaskData td, IOptionView v)
        {
            _td = td;
            _ov = v;
            _ov.RefreshTaskFieldsEvent += RefreshTasksfield;
            _ov.DeleteTaskFieldDomainEvent += DeleteTaskFieldDomain;
            _ov.RefreshRootPathEvent += RefreshRootPath;
            _ov.ChangeRootPathEvent += ChangeRootPath;
            _ov.RefreshStatusDomainEvent += RefreshStatusDomain;
            _ov.DeleteStatusDomainEvent += DeleteStatusDomain;
            _ov.UpdateStatusDomainEvent += UpdateStatusDomain;
        }

        private void UpdateStatusDomain(object sender, StatusDomain e)
        {
            _td.UpdateStatusDomain(e);
        }

        private void DeleteStatusDomain(object sender, StatusDomain e)
        {
            _td.DeleteStatusDomain(e);
        }

        private void RefreshStatusDomain(object sender, EventArgs e)
        {
            var d = new BindingList<StatusDomain>(_td.GetStatusDomainList());
            d.AddingNew += new AddingNewEventHandler(AddNewStatusDomain); 
            _ov.StatusDomainView = d;
        }

        private void AddNewStatusDomain(object sender, AddingNewEventArgs e)
        {
            e.NewObject = new StatusDomain();
        }

        public AppSetting appSetting { get; set; }

        private void ChangeRootPath(object sender, EventArgs e)
        {
            appSetting.SaveSaveAppSetting("FileRootPath", _ov.RootPath);
        }

        private void RefreshRootPath(object sender, EventArgs e)
        {
            _ov.RootPath = appSetting.GetAppsetting("FileRootPath");
        }

        private void DeleteTaskFieldDomain(object sender, ObjectAttribute e)
        {
            _td.DeleteTaskFieldDomain(e);
        }

        public IOptionView OptionView { get { return _ov; } }


        #region event handling
        
        public void RefreshTasksfield(object sender, EventArgs e)
        {
            var d = new BindingList<ObjectAttribute>(_td.GetTaskField());
            d.AddingNew += new AddingNewEventHandler(AddNewTaskField);
            _ov.TaskFieldView = d;
        }

        private void AddNewTaskField(object sender, AddingNewEventArgs e)
        {
            e.NewObject = new ObjectAttribute() { ObjectName = "task" };
        }

        #endregion
    }
}
